//
//  KZTimerPausingView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/27.
//  Copyright © 2018 zhukai. All rights reserved.
//

#import "KZTimerPausingView.h"

@implementation KZTimerPausingView
{
    UIView *_contentView;
}
- (void)show:(UIView *)view {
    
    UIView *superview = view ?: [UIApplication sharedApplication].keyWindow;

    self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.0];
    [superview addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superview);
    }];
    
    _contentView = [UIView new];
    _contentView.backgroundColor = UIColor.whiteColor;
    _contentView.layer.cornerRadius = 4;
    _contentView.layer.masksToBounds = true;
    [self addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self).multipliedBy(0.75);
        make.center.equalTo(self);
        make.height.mas_equalTo(160);
    }];
    
    UILabel *noteLabel = [[UILabel alloc] init];
    noteLabel.adjustsFontSizeToFitWidth = true;
    noteLabel.numberOfLines = 1;
    noteLabel.text = @"暂停中";
    noteLabel.font = FONT(40);
    noteLabel.textAlignment = NSTextAlignmentCenter;
    noteLabel.textColor = kGreenColor;
    [_contentView addSubview:noteLabel];
    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-H(40));
    }];
    
    UIButton *goonButton = [UIButton buttonWithType:UIButtonTypeCustom];
    goonButton.titleLabel.font = FONT(18);
    [goonButton setTitle:@"继续答题" forState:UIControlStateNormal];
    [goonButton setTitleColor:kBlueColor forState:UIControlStateNormal];
    [goonButton addTarget:self action:@selector(goon) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:goonButton];
    [goonButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.equalTo(noteLabel.mas_bottom);
    }];
    
    // 线条
    UIView *line = [UIView new];
    line.backgroundColor = kLineColor;
    [_contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(goonButton);
        make.height.equalTo(@1);
    }];
    
    
    
    [self layoutIfNeeded];
    
    _contentView.transform = CGAffineTransformMakeScale(0.001, 0.001);
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self->_contentView.transform = CGAffineTransformIdentity;
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.3];
    } completion:^(BOOL finished) {

    }];
}

- (void)hide {
    
    [UIView animateWithDuration:0.25 animations:^{
        self->_contentView.transform = CGAffineTransformMakeScale(0.001, 0.001);
        self.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.1];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)goon {
    
    [self hide];
    if (self.onClickGoon) {
        self.onClickGoon();
    }
}
@end
